<template>
  <div class="app-container">
    <div id="map"></div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="门店名称" prop="storeName">
        <el-input v-model="queryParams.storeName" placeholder="请输入门店名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="联系电话" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          v-hasPermi="['business:couponList:query']">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:couponList:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:couponList:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:couponList:del']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeList" max-height="650px" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="适用区域" align="center" prop="regionName" :show-overflow-tooltip="true" width="120">
        <!-- <template slot-scope="scope">
          <el-tooltip class="tooltip" placement="top">
            <div slot="content" style="max-width: 650px">
              {{ scope.row.regionName === null ? "无" : scope.row.regionName }}
            </div>
            <p class="descStyle">
              {{ scope.row.regionName === null ? "无" : scope.row.regionName }}
            </p>
          </el-tooltip>
        </template> -->
      </el-table-column>
      <el-table-column label="适用门店" :show-overflow-tooltip="true" align="center" prop="storeName">
        <!-- <template slot-scope="scope">
          <el-tooltip class="tooltip" placement="top">
            <div slot="content" style="max-width: 650px">
              {{ scope.row.storeName === null ? "无" : scope.row.storeName }}
            </div>
            <p class="descStyle">
              {{ scope.row.storeName === null ? "无" : scope.row.storeName }}
            </p>
          </el-tooltip>
        </template> -->
      </el-table-column>
      <!-- <el-table-column label="序号" align="center" prop="storeName" /> -->
      <el-table-column label="优惠券名称" align="center" prop="name" width="120" />
      <el-table-column label="优惠券类型" align="center" prop="discountsType" width="120">

        <template slot-scope="scope">
          <span>{{ scope.row.discountsType == 1 ? '折扣券' : '立减券' }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column
        label="优惠券图片"
        align="center"
        prop="picture"
        width="160"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.picture" :width="50" :height="50" />
        </template>
      </el-table-column> -->
      <el-table-column label="优惠门槛" align="center" prop="fullAmount" width="120" />
      <el-table-column label="优惠金额" align="center" prop="discountAmount" width="120">
        <template slot-scope="scope">
          <span>{{
            scope.row.discountAmount ? scope.row.discountAmount : "暂无"
          }}</span>
        </template>
      </el-table-column>

      <el-table-column label="折扣率" align="center" prop="discountRate" width="120" />

      <!-- <el-table-column label="适用项目" align="center" prop="projectName"  width="120"/> -->
      <el-table-column label="适用项目" :show-overflow-tooltip="true" align="center" prop="projectName">
        <!-- <template slot-scope="scope">
          <el-tooltip class="tooltip" placement="top">
            <div slot="content" style="max-width: 650px">
              {{
                scope.row.projectName === null ? "无" : scope.row.projectName
              }}
            </div>
            <p class="descStyle">
              {{
                scope.row.projectName === null ? "无" : scope.row.projectName
              }}
            </p>
          </el-tooltip>
        </template> -->
      </el-table-column>
      <el-table-column label="使用周期" align="center" prop="useToday" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.useToday ? scope.row.useToday : "暂无" }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark"  width="120"/> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.remark === null ? "无" : scope.row.remark }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="状态" align="center" prop="state" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.state == "1" ? "上架" : "下架" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="280">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleUpdate(scope.row, 'n')"
            v-hasPermi="['business:couponList:edit']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:couponList:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-upload2" @click="handleGround(scope.row, (state = 1))"
            v-hasPermi="['business:couponList:listing']" :disabled="scope.row.state === '1'">上架</el-button>
          <el-button size="mini" type="text" icon="el-icon-download" @click="handleGround(scope.row, (state = 0))"
            v-hasPermi="['business:couponList:offShelf']" :disabled="scope.row.state === '0'">下架</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:couponList:del']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 添加或修改门店对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px" :disabled="isNotEditor == 'n'">
        <!-- <el-form-item label="优惠券图片" prop="picture">
          <image-upload v-model="form.picture" />
        </el-form-item> -->

        <el-row>
          <el-col :span="12">
            <el-form-item label="优惠券名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入优惠券名称" style="width: 222px" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="优惠券类型" prop="couponTypeId">
              <el-select clearable v-model="form.couponTypeId">
                <el-option
                  v-for="(item, index) of type"
                  :key="index"
                  :label="item.value"
                  :value="item.key"
                  >{{ item.value }}</el-option
                >
              </el-select>
            </el-form-item>
          </el-col> -->
        </el-row>
        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="优惠券类型" prop="couponTypeValue">
              <el-radio-group v-model="form.couponTypeValue" @change="couponChange">
                <el-radio :label="1">平台券</el-radio>
                <el-radio :label="3">项目券</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row> -->
        <div v-if="form.couponTypeValue === 3">
          <el-row>
            <el-col :span="12">
              <el-form-item label="区域" prop="regionIds">
                <el-select clearable filterable multiple v-model="form.regionIds" @change="changeRegion">
                  <div class="checkboxWrapper">
                    <el-checkbox v-model="checked" @change.native="checkChange">
                      全选
                    </el-checkbox>
                  </div>
                  <el-option v-for="(item, index) of region" :key="index" :label="item.value" :value="item.key">
                    {{ item.value }}
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="适用门店" prop="storeIds">
                <el-select clearable filterable multiple v-model="form.storeIds" @change="changeStore">
                  <div class="checkboxWrapper">
                    <el-checkbox v-model="checked1" @change.native="checkStoreChange">
                      全选
                    </el-checkbox>
                  </div>
                  <el-option v-for="(item, index) of store" :key="index" :label="item.value" :value="item.key">{{
                    item.value
                  }}</el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目" prop="projectId">
                <el-select multiple clearable filterable v-model="form.projectId" @change="changeProject">
                  <div class="checkboxWrapper">
                    <el-checkbox v-model="checked2" @change.native="checkProjectChange">
                      全选
                    </el-checkbox>
                  </div>
                  <el-option v-for="(item, index) of project" :key="index" :label="item.value" :value="item.key">{{
                    item.value }}</el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="优惠类型" prop="discountsType">
              <el-radio-group v-model="form.discountsType" @change="handleChange">
                <el-radio :label="1">折扣券</el-radio>
                <el-radio :label="2">立减券</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 折扣优惠门槛 -->
        <div v-if="form.discountsType === 1">
          <el-row>
            <el-col :span="8">
              <el-form-item label="折扣比例" prop="discountRate">
                <el-input @input="
                  (value) =>
                    (this.form.discountRate = value.replace(/[^\d.]/g, ''))
                " v-model="form.discountRate" placeholder="请输入折扣比例" style="width: 222px" />
              </el-form-item>
            </el-col>
            <el-col :span="8" style="display: flex">
              <el-form-item label="最高优惠金额" prop="fullAmountRate">
                <div style="display: flex; align-items: center; height: 36px">
                  <el-input @input="
                    (value) =>
                    (this.form.fullAmountRate = value.replace(
                      /[^\d.]/g,
                      ''
                    ))
                  " v-model="form.fullAmountRate" placeholder="请输入最高优惠金额" style="width: 222px" />
                  <el-tooltip class="item" effect="dark" content="折扣券最多可抵扣多少钱，折扣金额超出最高优惠金额后按设置的最高优惠金额抵扣，0为不限制"
                    placement="top">
                    <i class="el-icon-warning" style="color: #f7453e"></i>
                  </el-tooltip>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="8" style="display: flex">
              <el-form-item label="满多少可用" prop="fullAmount1">
                <div style="display: flex; align-items: center; height: 36px">
                  <el-input v-model="form.fullAmount1" placeholder="请输入" style="width: 222px" />
                  <el-tooltip class="item" effect="dark" content="0为不限制" placement="top">
                    <i class="el-icon-warning" style="color: #f7453e"></i>
                  </el-tooltip>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <!-- 立减优惠门槛 -->
        <div v-if="form.discountsType === 2">
          <el-row>
            <el-col :span="12" style="display: flex">
              <el-form-item label="立减优惠门槛">
                <el-input @input="
                  (value) =>
                    (this.form.fullAmount2 = value.replace(/[^\d.]/g, ''))
                " v-model="form.fullAmount2" placeholder="请输入立减优惠门槛" style="width: 222px" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="优惠金额" prop="discountAmount">
                <el-input @input="
                  (value) =>
                    (this.form.discountAmount = value.replace(/[^\d.]/g, ''))
                " v-model="form.discountAmount" placeholder="请输入优惠金额" style="width: 222px" />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单次赠送张数" prop="oneGiveCount">
              <el-input @input="
                (value) =>
                  (this.form.oneGiveCount = value.replace(/[^\d]/g, ''))
              " v-model="form.oneGiveCount" placeholder="请输入单次赠送张数" style="width: 222px" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="每天可使用张数" prop="totalUseCount">
              <div style="display: flex; align-items: center; height: 36px">
                <el-input @input="
                  (value) =>
                    (this.form.totalUseCount = value.replace(/[^\d]/g, ''))
                " v-model="form.totalUseCount" placeholder="请输入每天可使用张数" style="width: 222px" />
                <el-tooltip class="item" effect="dark" content="此规则下的优惠券每天可使用多少张，超出设置张数将不可使用，0为不限制" placement="top">
                  <i class="el-icon-warning" style="color: #f7453e"></i>
                </el-tooltip>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="领券间隔天数" prop="intervalCount">
              <div style="display: flex; align-items: center; height: 36px">
                <el-input @input="
                  (value) =>
                    (this.form.intervalCount = value.replace(/[^\d]/g, ''))
                " v-model="form.intervalCount" placeholder="请输入领券间隔天数" style="width: 222px" />
                <el-tooltip class="item" effect="dark" content="此规则下的优惠券间隔多少天可领取一次，默认不填为不限制，填数字几天就间隔几天可以领券"
                  placement="top">
                  <i class="el-icon-warning" style="color: #f7453e"></i>
                </el-tooltip>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户总领券次数限制" prop="userReceiveCount">
              <el-input @input="
                (value) =>
                  (this.form.userReceiveCount = value.replace(/[^\d]/g, ''))
              " v-model="form.userReceiveCount" placeholder="请输入次数" style="width: 222px" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="领取路径">
              <el-select clearable multiple v-model="getPath">
                <el-option label="【小程序首页】及小程序首页弹框" value="1"></el-option>
                <el-option label="【项目页】及小程序选择项目页面弹框" value="2"></el-option>
                <el-option label="【手动领取】用于其他需要配置优惠券或手动发放优惠券的地方使用" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="到期天数" prop="effectiveDays">
              <el-input @input="
                (value) =>
                  (this.form.effectiveDays = value.replace(/[^\d]/g, ''))
              " v-model="form.effectiveDays" placeholder="请输入到期天数" style="width: 222px" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="单日领取次数" prop="singleReceive">
              <el-input @input="
                (value) =>
                  (this.form.singleReceive = value.replace(/[^\d]/g, ''))
              " v-model="form.singleReceive" placeholder="请输入单日领取次数" style="width: 222px" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="使用时间类型" prop="useTimeType">
              <el-radio-group v-model="form.useTimeType" @change="timeTypeChange">
                <el-radio :label="1">全天</el-radio>
                <el-radio :label="2">时间段</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" v-if="form.useTimeType === 2">
            <el-form-item>
              <el-radio-group v-model="form.useTimeAstrict">
                <el-radio :label="1">按下单时间</el-radio>
                <el-radio :label="2">按服务开始时间</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" v-if="form.useTimeType === 2">
            <el-form-item>
              <el-radio-group v-model="form.userTime" @change="userTimeChange">
                <el-radio :label="1">周期</el-radio>
                <el-radio :label="2">日期</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <div v-if="form.useTimeType === 2 && form.userTime === 1">
            <el-col :span="12">
              <el-form-item label="使用周期" prop="useToday">
                <el-select clearable multiple v-model="form.useToday">
                  <el-option v-for="(item, index) of week" :key="index" :label="item.value" :value="item.key">{{
                    item.value
                  }}</el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </div>
          <div v-if="form.useTimeType === 2 && form.userTime === 2">
            <el-col :span="12">
              <el-form-item label="使用日期" prop="useDay">
                <!-- <el-input
                  v-model="form.fullAmount"
                  placeholder="请输入日期"
                  style="width: 222px"
                /> -->
                <!-- <el-date-picker
                  v-model="form.useDay"
                  type="date"
                  placeholder="选择日期"
                >
                </el-date-picker> -->
                <el-select clearable multiple v-model="form.useDay">
                  <div class="checkboxWrapper">
                    <el-checkbox v-model="checked3" @change.native="checkDay">
                      全选
                    </el-checkbox>
                  </div>
                  <el-option v-for="(item, index) of Day" :key="index" :label="item.value" :value="item.key">{{
                    item.value
                  }}</el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </div>
        </el-row>
        <el-row v-if="form.useTimeType === 2">
          <el-form-item label="使用时间" prop="ableUseStartTime">
            <!-- <el-time-select
              placeholder="起始时间"
              v-model="form.ableUseStartTime"
              :picker-options="{
                start: '08:00',
                step: '00:15',
                end: '23:59',
              }"
            >
            </el-time-select> -->
            <el-time-picker v-model="form.ableUseStartTime" value-format="HH:mm" format="HH:mm" placeholder="起始时间">
            </el-time-picker>
            --
            <el-time-picker v-model="form.ableUseEndTime" value-format="HH:mm" format="HH:mm" placeholder="结束时间">
            </el-time-picker>
            <!-- <el-time-select
              placeholder="结束时间"
              v-model="form.ableUseEndTime"
              :picker-options="{
                start: '08:00',
                step: '00:15',
                end: '23:59',
                minTime: form.ableUseStartTime,
              }"
            >
            </el-time-select> -->
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="叠加使用类型">
              <el-checkbox-group v-model="form.overLayUseType">
                <el-checkbox label="2" name="type">会员权益可用</el-checkbox>
                <el-checkbox label="3" name="type">储值权益可用</el-checkbox>
                <el-checkbox label="4" name="type">其他支付可用</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="使用限制">
              <el-checkbox-group v-model="form.useLimit">
                <el-checkbox label="1" name="type">自用</el-checkbox>
                <el-checkbox label="2" name="type">赠送</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否上架" prop="state">
              <el-select clearable v-model="form.state">
                <el-option v-for="(item, index) of List" :key="index" :label="item.value" :value="item.key">{{
                  item.value
                }}</el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                placeholder="请输入备注"
                style="width: 222px"
              />
            </el-form-item>
          </el-col>  -->
        </el-row>
        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="优惠门槛" prop="fullAmount">
              <el-input
                v-model="form.fullAmount"
                placeholder="请输入优惠券类型"
                style="width: 222px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="优惠金额" prop="discountAmount">
              <el-input
                v-model="form.discountAmount"
                placeholder="请输入优惠券类型"
                style="width: 222px"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="适用项目" prop="projectId">
              <el-select multiple clearable v-model="form.projectId">
                <el-option
                  v-for="(item, index) of project"
                  :key="index"
                  :label="item.value"
                  :value="item.key"
                  >{{ item.value }}</el-option
                >
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用周期" prop="useToday">
              <el-select clearable multiple v-model="form.useToday">
                <el-option
                  v-for="(item, index) of week"
                  :key="index"
                  :label="item.value"
                  :value="item.key"
                  >{{ item.value }}</el-option
                >
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="可领取开始时间" prop="receiveStartTime">
              <el-date-picker
                v-model="form.receiveStartTime"
                type="date"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="可领取结束时间" prop="receiveEndTime">
              <el-date-picker
                v-model="form.receiveEndTime"
                type="date"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="优惠券到期时间" prop="effectiveTime">
          <el-date-picker
            v-model="form.effectiveTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="总量" prop="total">
              <el-input
                v-model="form.total"
                placeholder="请输入总量"
                style="width: 222px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                placeholder="请输入备注"
                style="width: 222px"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="是否上架" prop="state">
          <el-select clearable v-model="form.state">
            <el-option
              v-for="(item, index) of List"
              :key="index"
              :label="item.value"
              :value="item.key"
              >{{ item.value }}</el-option
            >
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="isNotEditor != 'n'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listcoupon,
  listtype,
  getcoupon,
  delitecoupon,
  addcoupon,
  updatecoupon,
  listMemberRegion,
  updatecouponsssss,
  listMemberStore,
  listslot,
  listitem,
} from "@/api/system/coupon/couponList";

export default {
  name: "Store",
  data() {
    return {
      map: null,
      local: null,
      mk: null,
      latitude: "",
      longitude: "",
      keyWords: "",
      getPath: [],
      result: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门店表格数据
      storeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeName: null,
        phoneNumber: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
        detail: null,
        longitude: null,
        latitude: null,
        intro: null,
        images: null,
        status: null,
        isInitiate: null,
        openingTime: null,
        closingTime: null,
      },
      // 表单参数
      form: {
        discountsType: 1,
        couponTypeValue: 3,
        useTimeType: 1,
        overLayUseType: [],
        useLimit: [],
        useLimits: 1,
        userTime: 1,
        useTimeAstrict: 1,
        state: "1",
        singleReceive: "",
        ableUseStartTime: null,
        ableUseEndTime: null,
        regionIds: [],
        storeIds: [],
        projectId: [],
        useDay: [],
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入优惠券名称", trigger: "blur" },
        ],
        regionIds: [{ required: true, message: "请选择区域", trigger: "blur" }],
        sheets: [
          { required: true, message: "请输入每天可使用张数", trigger: "blur" },
        ],
        storeIds: [
          { required: true, message: "请选择适用门店", trigger: "blur" },
        ],
        // couponTypeId: [
        //   { required: true, message: "请选择优惠券类型", trigger: "blur" },
        // ],
        // picture: [
        //   { required: true, message: "请上传优惠券图片", trigger: "blur" },
        // ],
        discountRate: [
          { required: true, message: "请输入", trigger: "blur" },
          {
            pattern: /^[0-9]+(\.[0-9]+)?$/,
            message: "请输入",
            trigger: "blur",
          },
        ],
        fullAmountRate: [
          { required: true, message: "请输入", trigger: "blur" },
          {
            pattern: /^[0-9]+(\.[0-9]+)?$/,
            message: "请输入",
            trigger: "blur",
          },
        ],
        fullAmount1: [
          { required: true, message: "请输入优惠门槛", trigger: "blur" },
          {
            pattern: /^[0-9]+(\.[0-9]+)?$/,
            message: "请输入优惠门槛",
            trigger: "blur",
          },
        ],
        fullAmount2: [
          { required: true, message: "请输入优惠门槛", trigger: "blur" },
          {
            pattern: /^[0-9]+(\.[0-9]+)?$/,
            message: "请输入优惠门槛",
            trigger: "blur",
          },
        ],
        discountAmount: [
          { required: true, message: "请输入优惠金额", trigger: "blur" },
          {
            pattern: /^[0-9]+(\.[0-9]+)?$/,
            message: "请输入优惠门槛",
            trigger: "blur",
          },
        ],
        discountsType: [{ required: true, message: "请选择", trigger: "blur" }],
        couponTypeValue: [
          { required: true, message: "请选择", trigger: "blur" },
        ],
        useTimeType: [{ required: true, message: "请输入", trigger: "blur" }],
        projectId: [
          { required: true, message: "请输入适用项目", trigger: "blur" },
        ],
        useToday: [
          { required: true, message: "请输入使用周期", trigger: "blur" },
        ],
        useDay: [
          { required: true, message: "请输入使用日期", trigger: "blur" },
        ],
        receiveStartTime: [
          { required: true, message: "请选择时间", trigger: "blur" },
        ],
        // receiveEndTime: [
        //   { required: true, message: "请输入可领取结束时间", trigger: "blur" },
        // ],
        effectiveTime: [
          { required: true, message: "请输入优惠券到期时间", trigger: "blur" },
        ],
        total: [
          { required: true, message: "请输入总量", trigger: "blur" },
          {
            pattern: /^[0-9]+(\.[0-9]+)?$/,
            message: "请输入优惠门槛",
            trigger: "blur",
          },
        ],
        state: [{ required: true, message: "请输入上架", trigger: "blur" }],
      },
      options: [],
      List: [
        {
          key: "1",
          value: "上架",
        },
        {
          key: "0",
          value: "下架",
        },
      ],
      region: [],
      store: [],
      time: [],
      week: [
        {
          key: "周一",
          value: "周一",
        },
        {
          key: "周二",
          value: "周二",
        },
        {
          key: "周三",
          value: "周三",
        },
        {
          key: "周四",
          value: "周四",
        },
        {
          key: "周五",
          value: "周五",
        },
        {
          key: "周六",
          value: "周六",
        },
        {
          key: "周日",
          value: "周日",
        },
      ],
      Day: [
        { key: "1", value: "1号" },
        { key: "2", value: "2号" },
        { key: "3", value: "3号" },
        { key: "4", value: "4号" },
        { key: "5", value: "5号" },
        { key: "6", value: "6号" },
        { key: "7", value: "7号" },
        { key: "8", value: "8号" },
        { key: "9", value: "9号" },
        { key: "10", value: "10号" },
        { key: "11", value: "11号" },
        { key: "12", value: "12号" },
        { key: "13", value: "13号" },
        { key: "14", value: "14号" },
        { key: "15", value: "15号" },
        { key: "16", value: "16号" },
        { key: "17", value: "17号" },
        { key: "18", value: "18号" },
        { key: "19", value: "19号" },
        { key: "20", value: "20号" },
        { key: "21", value: "21号" },
        { key: "22", value: "22号" },
        { key: "23", value: "23号" },
        { key: "24", value: "24号" },
        { key: "25", value: "25号" },
        { key: "26", value: "26号" },
        { key: "27", value: "27号" },
        { key: "28", value: "28号" },
        { key: "29", value: "29号" },
        { key: "30", value: "30号" },
        { key: "31", value: "31号" },
      ],
      type: [],
      project: [],
      checked: false,
      checked1: false,
      checked2: false,
      checked3: false,
      isNotEditor: "y",
    };
  },
  watch: {
    // "form.regionIds"(newVal, oldVal) {
    //   if (newVal.length > 0) {
    //     const regionIds = newVal.join(",");
    //     console.log(regionIds,'regionIds')
    //     this.$nextTick(() => {
    //       listMemberStore({ regionIds }).then((res) => {
    //         this.store = res.data.map((rItem) => {
    //           return {
    //             key: rItem.id,
    //             value: rItem.storeName,
    //           };
    //         });
    //       });
    //     });
    //   }
    // },
    "form.regionIds": {
      handler: function (newVal, oldVal) {
        // console.log(newVal, oldVal,'newVal, oldVal');
        if (newVal.length > 0) {
          const regionIds = newVal.join(",");
          this.$nextTick(() => {
            listMemberStore({ regionIds }).then((res) => {
              this.store = res.data.map((rItem) => {
                return {
                  key: rItem.id,
                  value: rItem.storeName,
                };
              });
            });
          });
        }
      },
    },
    "form.storeIds": {
      handler: function (newVal) {
        if (newVal.length > 0) {
          const storeIds = newVal.join(",");
          this.$nextTick(() => {
            listitem({ storeIds }).then((res) => {
              this.project = res.data.map((rItem) => {
                return {
                  key: rItem.id,
                  value: rItem.itemName,
                };
              });
            });
          });
        }
      },
    },
  },
  created() {
    this.getList();
    this.listregion();
    this.listslot();
    this.listtype();
  },
  methods: {
    //使用日期全选change事件
    checkDay(e) {
      let resValues = this.Day.map((item) => item.key);
      console.log(resValues, "9090909090900");
      // 如果checkbox为false，则为反选，选择数组为空
      if (!e.target.checked) {
        this.form.useDay = [];
      } else {
        // 如果checkbox为true，则为全选，选择数组添加所有数据
        let res = [...resValues];
        this.form.useDay = res;
      }
    },
    //区域全选change事件
    checkChange(e) {
      let resValues = this.region.map((item) => item.key);
      console.log(
        e.target.checked,
        this.checked,
        "11111",
        this.form.regionIds,
        this.region
      );

      // 如果checkbox为false，则为反选，选择数组为空
      if (!this.checked) {
        this.form.regionIds = [];
      } else {
        // 如果checkbox为true，则为全选，选择数组添加所有数据
        let res = [...resValues];
        this.form.regionIds = res;
        console.log(res, this.form.regionIds, "222");
      }
    },
    //门店全选change事件
    checkStoreChange(e) {
      let resValues = this.store.map((item) => item.key);
      // 如果checkbox为false，则为反选，选择数组为空
      // 如果checkbox为true，则为全选，选择数组添加所有数据

      if (e.target.checked == false) {
        this.form.storeIds = [];
      } else {
        // 如果checkbox为true，则为全选，选择数组添加所有数据
        let res = [...resValues];
        this.form.storeIds = res;
      }
    },
    //项目全选change事件
    checkProjectChange(e) {
      let resValues = this.project.map((item) => item.key);
      // 如果checkbox为false，则为反选，选择数组为空
      if (!e.target.checked) {
        this.form.projectId = [];
      } else {
        // 如果checkbox为true，则为全选，选择数组添加所有数据
        let res = [...resValues];
        this.form.projectId = res;
      }
    },
    getDateWeek() {
      const now = new Date();
      const nowTime = now.getTime();
      const day = now.getDay();
      const oneDayTime = 24 * 60 * 60 * 1000;
      const mondayTime = nowTime - (day - 2) * oneDayTime; //默认是周二
      this.weeklyDate = new Date(mondayTime);
      this.newDateWeekHandle();
    },
    newDateWeekHandle() {
      const now = new Date(this.weeklyDate);
      const nowTime = now.getTime();
      const day = now.getDay();
      const oneDayTime = 24 * 60 * 60 * 1000;
      const mondayTime = nowTime - (day - 1) * oneDayTime;
      const sundayTime = nowTime + (7 - day) * oneDayTime;
      this.startDate = this.$moment(mondayTime).format("YYYY-MM-DD");
      this.endDate = this.$moment(sundayTime).format("YYYY-MM-DD");
    },
    listtype() {
      listtype().then((res) => {
        this.type = res.rows.map((rItem) => {
          return {
            key: rItem.id,
            value: rItem.typeName,
          };
        });
      });
    },
    listslot() {
      listslot().then((res) => {
        // console.log(res,888);
        this.time = res.rows.map((rItem) => {
          return {
            key: rItem.id,
            value: rItem.useSlot,
          };
        });
      });
    },
    listregion() {
      listMemberRegion().then((res) => {
        this.region = res.data.map((rItem) => {
          return {
            key: rItem.id,
            value: rItem.regionName,
          };
        });
      });
    },
    changeRegion(value) {
      // const selectedOption = this.region.find((item) => item.key === value);
      // console.log(selectedOption,'selectedOption666')
      // const selectedKey = selectedOption.key;
      // const selectedValue = selectedOption.value;
      // console.log(`Selected Key: ${selectedKey}`);
      // console.log(`Selected Value: ${selectedValue}`);
      // this.form.regionName = selectedValue;
      const resValues = this.region.map((item) => item.key);
      console.log(resValues, value.length, "changeRegion111");

      if (value.length === resValues.length) {
        this.checked = true;
      } else {
        this.checked = false;
      }
      this.form.regionIds = value;
      if (value) {
        this.form.storeIds = [];
      }
    },
    //门店change事件
    changeStore(value) {
      // const selectedOption = this.store.find((item) => item.key === value);
      // const selectedKey = selectedOption.key;
      // const selectedValue = selectedOption.value;
      // console.log(`Selected Key: ${selectedKey}`);
      // console.log(`Selected Value: ${selectedValue}`);
      // this.form.storeName = selectedValue;
      // listitem({ storeId: value }).then((res) => {
      //   this.project = res.rows.map((rItem) => {
      //     return {
      //       key: rItem.id,
      //       value: rItem.itemName,
      //     };
      //   });
      //   console.log(this.project, 999);
      // });
      const resValues = this.store.map((item) => item.key);
      if (value.length === resValues.length) {
        this.checked1 = true;
      } else {
        this.checked1 = false;
      }
      this.form.storeIds = value;
      if (value) {
        this.form.projectId = [];
      }
    },
    //项目change事件
    changeProject(value) {
      const resValues = this.project.map((item) => item.key);
      if (value.length === resValues.length) {
        this.checked2 = true;
      } else {
        this.checked2 = false;
      }
      this.form.projectId = value;
    },
    /** 查询门店列表 */
    getList() {
      this.loading = true;
      listcoupon(this.queryParams).then((response) => {
        this.storeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeName: null,
        phoneNumber: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
        detail: null,
        longitude: null,
        latitude: null,
        intro: null,
        images: null,
        status: null,
        isInitiate: null,
        openingTime: null,
        closingTime: null,
        createTime: null,
        updateTime: null,
        discountsType: 1,
        couponTypeValue: 3,
        discountRate: null,
        fullAmountRate: null,
        fullAmount: null,
        fullAmount1: null,
        fullAmount2: null,
        discountAmount: null,
        oneGiveCount: null,
        totalUseCount: null,
        intervalCount: "",
        effectiveDays: null,
        singleReceive: "",
        useToday: null,
        useDay: [],
        regionIds: [],
        storeIds: [],
        projectId: [],
        useTimeType: 1,
        overLayUseType: [],
        useLimit: [],
        userTime: 1,
        useTimeAstrict: 1,
        state: "1",
        ableUseStartTime: null,
        ableUseEndTime: null,
      };
      this.resetForm("form");
      this.checked = false;
      this.checked1 = false;
      this.checked2 = false;
      this.checked3 = false;
      this.getPath = []
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠券";
      this.isNotEditor = "y";
      console.log(
        this.checked,
        this.checked1,
        this.checked2,
        this.checked3,
        "111111"
      );
    },
    // 上下架
    handleGround(row, state) {
      const params = {
        id: row.id,
        state: state,
      };
      this.$confirm(
        `此操作将${state === 1 ? "上架" : "下架"}该数据, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          updatecouponsssss(params).then((res) => {
            this.$message({
              type: "success",
              message: `${state === 1 ? "上架" : "下架"}成功!`,
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消上下架",
          });
        });
    },
    //切换
    handleChange(val) {
      if (val === 1) {
        this.form.fullAmount2 = "";
        this.form.discountAmount = "";
        if (this.form) {
          this.form.fullAmount1 = this.form.fullAmount;
          this.form.discountRate = this.form.discountRate;
          this.form.fullAmountRate = this.form.fullAmountRate;
        }
      } else {
        this.form.fullAmount1 = "";
        this.form.discountRate = "";
        this.form.fullAmountRate = "";
      }
      this.$refs.form.clearValidate();
    },
    //使用时间类型change事件
    timeTypeChange(val) {
      if (val === 1) {
        this.form.userTime = "";
        this.form.useToday = "";
        this.form.useDay = "";
        this.form.ableUseStartTime = "";
        this.form.ableUseEndTime = "";
      }
    },
    //选择周期或者日期change事件
    userTimeChange(val) {
      console.log(val, "7878787878");
      if (val === 1) {
        this.form.useDay = "";
        this.form.ableUseStartTime = "";
        this.form.ableUseEndTime = "";
      } else {
        this.form.useToday = "";
        this.form.ableUseStartTime = "";
        this.form.ableUseEndTime = "";
      }
    },
    //优惠券类型change事件
    couponChange(val) {
      // if(val === 1){
      //   this.form.regionIds = ''
      //   this.form.storeIds = ''
      //   this.form.projectId = ''
      // }
    },
    /** 修改按钮操作 */
    handleUpdate(row, flag = "y") {
      this.reset();
      const id = row.id || this.ids;
      this.isNotEditor = flag;
      getcoupon(id).then((response) => {


        if (response.data.regionIds == "") {
          response.data.regionIds = null;
        } else {
          response.data.regionIds = response.data.regionIds
            .split(",")
            .map(Number);
        }
        if (response.data.storeIds == "") {
          response.data.storeIds = null;
        } else {
          response.data.storeIds = response.data.storeIds
            .split(",")
            .map(Number);
        }
        if (response.data.projectId == "") {
          response.data.projectId = null;
        } else {
          response.data.projectId = response.data.projectId
            .split(",")
            .map(Number);
        }
        if (response.data.useTimeType === 1) {
          this.form.useToDay = "";
        } else {
          this.form.useDay = "";
        }
        this.form.discountsType === 1
          ? (this.form.fullAmount1 = response.data.fullAmount)
          : (this.form.fullAmount2 = response.data.fullAmount);
          
          this.form.fullAmount1 = response.data.fullAmount
          this.form.fullAmount2 = response.data.fullAmount

        response.data.useToday = response.data.useToday
          ? response.data.useToday.split(",")
          : "";

        // if (Array.isArray(response.data.useDay)) {
        //   response.data.useDay = response.data.useDay
        //     ? response.data.useDay.split(",")
        //     : "";
        // }
        if (response.data.useDay) {
          response.data.useDay = response.data.useDay
            ? response.data.useDay.split(",")
            : "";
        }

        response.data.overLayUseType = response.data.overLayUseType
          ? response.data.overLayUseType.split(",")
          : "";
        response.data.useLimit = response.data.useLimit
          ? response.data.useLimit.split(",")
          : "";

        this.getPath = response.data.getPath.split(",");
        response.data.couponTypeValue = Number(response.data.couponTypeValue);
        response.data.couponTypeName = "";
        this.open = true;
        this.title = "优惠券详情";
        this.form = response.data;
        this.form.useTimeAstrict = response.data.useTimeAstrict * 1;
        // listitem({ storeId: response.data.storeId }).then((res) => {
        //   console.log(res, 123);
        //   this.project = res.rows.map((rItem) => {
        //     return {
        //       key: rItem.id,
        //       value: rItem.itemName,
        //     };
        //   });
        // });
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        let data = {};
        if (valid) {
          if (this.form.id != null) {
            data = {
              ...this.form,
              useToday: this.form.useToday
                ? this.form.useToday.join(",")
                : null,
              getPath: this.getPath ? this.getPath.join(",") : null,
              useDay: this.form.useDay ? this.form.useDay.join(",") : null,
              fullAmount:
                this.form.discountsType === 1
                  ? this.form.fullAmount1
                  : this.form.fullAmount2, 
            };
            if (Array.isArray(this.form.overLayUseType)) {
              data.overLayUseType = this.form.overLayUseType
                ? this.form.overLayUseType.join(",")
                : null;
            }
            if (Array.isArray(this.form.useLimit)) {
              data.useLimit = this.form.useLimit
                ? this.form.useLimit.join(",")
                : null;
            }
            if (this.form.regionIds && Array.isArray(this.form.regionIds)) {
              data.regionIds = this.form.regionIds.join(",");
            }
            if (this.form.storeIds && Array.isArray(this.form.storeIds)) {
              data.storeIds = this.form.storeIds.join(",");
            }
            if (this.form.projectId && Array.isArray(this.form.projectId)) {
              data.projectId = this.form.projectId.join(",");
            }
            updatecoupon(data).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            data = {
              ...this.form,
              getPath: this.getPath ? this.getPath.join(",") : null,
              useToday: this.form.useToday
                ? this.form.useToday.join(",")
                : null,
              useDay: this.form.useDay ? this.form.useDay.join(",") : null,
              overLayUseType: this.form.overLayUseType
                ? this.form.overLayUseType.join(",")
                : null,
              useLimit: this.form.useLimit
                ? this.form.useLimit.join(",")
                : null,
              regionIds: this.form.regionIds
                ? this.form.regionIds.join(",")
                : null,
              storeIds: this.form.storeIds
                ? this.form.storeIds.join(",")
                : null,
              projectId: this.form.projectId
                ? this.form.projectId.join(",")
                : null,
              fullAmount:
                this.form.discountsType === 1
                  ? this.form.fullAmount1
                  : this.form.fullAmount2,
            };
            addcoupon(data).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.open = false;
              }
            });
          }
        }
      });
    },
    handReset(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认重置编号为"' + ids + '"的数据项？')
        .then(function () {
          return listResetPwd({ userId: ids });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("重置成功");
        })
        .catch(() => { });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除编号为"' + ids + '"的数据项？')
        .then(function () {
          return delitecoupon(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/store/export",
        {
          ...this.queryParams,
        },
        `store_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss">
.checkboxWrapper {
  padding: 8px 20px;
  border-bottom: 1px solid #ccc;
}

.descStyle {
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.tooltip {
  max-width: 650px;
}
</style>
